home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************************
- Color Fractal Generator 2.1
- module: CFG Palette Skeleton.c
- September 1992 - March 1994
- by John A. Schlack
-
- Description:
- This module provides a guideline for creating your own CFG palettes.
- All that needs to be done is to fill in the code in create_palette() so that
- the desired colors are created.
-
- **************************************************************************************/
-
-
- #include <string.h>
-
-
- /**************************************************************************************
- TYPEDEFS
- **************************************************************************************/
-
-
- typedef struct identifier_struct // 128 byte header
- {
- char zero[4]; // first 4 bytes must be 0
- char appl_name[32]; // application name
- char by[32];
- OSType file_type; // fractal, palette, cusfrac, prefs, etc.
- char free1[4]; // used by ver 2.0 as part of 8 byte file_type
- OSType version; // application version: x.xx
- OSType earliest_vers; // earliest app version that can open file
- char registered[4];
- char password[8];
- char free2[32];
- } identifier_struct, *identifier_ptr;
-
- typedef struct file_palette_header_210
- {
- short ctSize; // number of entries in color table - 1
- short userColors; // # colors user defined
- Str255 load_pal_name; // name of palette
- } file_palette_header_210;
-
-
- /**************************************************************************************
- REGIONAL VARIABLES
- **************************************************************************************/
-
-
- static char file_cmp[4] = { '2', '.', '0', '0' };
- static char file_erl[4] = { '2', '.', '0', '0' };
-
-
- /**************************************************************************************
- CONSTANTS
- **************************************************************************************/
-
-
- #define FILE_CREATOR 'fraG'
- #define FILE_TYPE_PALETTE 'fraP'
- #define CFG_VERSION '2.10'
- #define CFG_EARLIEST '2.10'
- #define FT_PALETTE 'pltt'
-
- #define STD_PALETTE_SIZE 256
- #define MAX_USER_COLORS 250
-
-
- // THESE ARE OK TO CHANGE: BUT A MAX OF 32 BYTES EACH
-
- char * gStr_file_appl_name = "Color Fractal Generator";
- char * gStr_file_by = "John A. Schlack Shadowbane";
-
-
- /**************************************************************************************
- PROTOTYPES
- **************************************************************************************/
-
-
- void main( void );
- void write_palette_to_file( unsigned char * name, short vRefNum,
- RGBColor * entries );
- void create_palette( RGBColor * entries );
- void add_standard_colors( RGBColor * entries );
- unsigned char * pstrcpy( unsigned char * dest, unsigned char * src );
- void do_alert( unsigned char * msg );
-
-
- /* --------------------------------------------------------------------------------- */
-
-
- void main( void )
- {
- Point thePoint = { 40, 40 };
- SFReply reply;
- RGBColor * entries;
-
- InitGraf( &thePort );
- InitFonts();
- FlushEvents( everyEvent, 0 );
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- InitCursor();
-
- /* allocate memory */
-
- entries = (RGBColor *) NewPtr( STD_PALETTE_SIZE * sizeof( RGBColor ) );
- if (entries == NULL)
- {
- do_alert( "\pNot enough memory to create palette" );
- ExitToShell();
- }
-
- /* keep creating palettes until user hits cancel button */
- for (;;)
- {
- SFPutFile( thePoint, "\pEnter Palette Name", "\pPalette", 0L, &reply );
- if (!(reply.good))
- break;
- memset( entries, 0, sizeof( RGBColor ) * STD_PALETTE_SIZE );
- create_palette( entries + 1 ); /* +1 skips reserved "white" */
- add_standard_colors( entries );
- write_palette_to_file( reply.fName, reply.vRefNum, entries );
- }
-
- /* free memory */
- DisposPtr( (Ptr) entries );
- }
-
-
- /* -------------------------------------------------------------------------------- */
-
-
- void write_palette_to_file( unsigned char * name, short vRefNum, RGBColor * entries )
- {
- identifier_struct ID;
- file_palette_header_210 pal_head;
- long siz;
- short srcFile;
- OSErr theErr;
-
- /* create the palette file */
-
- theErr = Create( name, vRefNum, FILE_CREATOR, FILE_TYPE_PALETTE );
- if ((theErr != noErr) && (theErr != dupFNErr))
- {
- do_alert( "\pCannot create palette file" );
- return;
- }
-
- /* open the file for writing */
-
- theErr = FSOpen( name, vRefNum, &srcFile );
- if (theErr != noErr)
- {
- do_alert( "\pCannot open palette file" );
- FSDelete( name, vRefNum );
- return;
- }
-
- // ID
-
- memset( &ID, 0, sizeof( ID ) );
- strcpy( ID.appl_name, gStr_file_appl_name );
- strcpy( ID.by, gStr_file_by );
- ID.file_type = FT_PALETTE;
- ID.version = CFG_VERSION;
- ID.earliest_vers = CFG_EARLIEST;
-
- // FILL PALETTE HEADER
-
- pal_head.ctSize = STD_PALETTE_SIZE;
- pal_head.userColors = MAX_USER_COLORS;
- pstrcpy( pal_head.load_pal_name, name );
-
- // WRITE ID
-
- siz = (long) sizeof( identifier_struct );
- theErr = FSWrite( srcFile, &siz, &ID );
-
- // WRITE PALETTE HEADER
-
- if (theErr == noErr)
- {
- siz = (long) sizeof( file_palette_header_210 );
- theErr = FSWrite( srcFile, &siz, &pal_head );
- }
-
- // WRITE PALETTE
-
- if (theErr == noErr)
- {
- siz = sizeof( RGBColor ) * (pal_head.ctSize + 1L);
- theErr = FSWrite( srcFile, &siz, entries );
- }
-
- // CLEAN UP
-
- GetFPos( srcFile, &siz );
- SetEOF( srcFile, siz );
- FSClose( srcFile );
-
- if (theErr != noErr)
- {
- do_alert( "\pError while writing to palette file" );
- FSDelete( name, vRefNum );
- }
-
- FlushVol( 0L, vRefNum );
- }
-
-
- /* --------------------------------------------------------------------------------- */
-
-
- void create_palette( RGBColor * entries )
- {
- short i;
- RGBColor rgb;
-
- /*
- Do not change the loop counter of index to entries. These values are correct.
- All that needs to be done is to change rgb to the desired color corresponding
- to the palette entry i.
- */
- for (i=0; i<MAX_USER_COLORS; i++)
- {
- /* •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- SET "rgb" TO DESIRED COLOR
- ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• */
- rgb.red = rgb.green = rgb.blue = 0;
-
- entries[i] = rgb;
- }
- }
-
-
- /* --------------------------------------------------------------------------------- */
-
-
- void add_standard_colors( RGBColor * entries )
- {
- RGBColor rgb;
-
- rgb.red = rgb.blue = rgb.green = 0xFFFF; /* white is first entry */
- entries[0] = rgb;
-
- rgb.red = rgb.blue = rgb.green = 0; /* reserved */
- entries[STD_PALETTE_SIZE-5] = rgb;
-
- rgb.red = rgb.blue = rgb.green = 0xD000; /* light gray */
- entries[STD_PALETTE_SIZE-4] = rgb;
-
- rgb.red = rgb.blue = rgb.green = 0xA000; /* medium gray */
- entries[STD_PALETTE_SIZE-3] = rgb;
-
- rgb.red = rgb.blue = rgb.green = 0x5000; /* dark gray */
- entries[STD_PALETTE_SIZE-2] = rgb;
-
- rgb.red = rgb.blue = rgb.green = 0; /* black is last entry */
- entries[STD_PALETTE_SIZE-1] = rgb;
- }
-
-
- /* -------------------------------------------------------------------------------- */
-
-
- unsigned char * pstrcpy( unsigned char * dest, unsigned char * src )
- {
- register short i = *src, j;
- unsigned char * ret = dest;
-
- for (j=0; j<=i; j++) /* must include size byte, so has equal sign */
- *dest++ = *src++;
- return dest;
- }
-
-
- /* -------------------------------------------------------------------------------- */
-
-
- void do_alert( unsigned char * msg )
- {
- ParamText( msg, 0L, 0L, 0L );
- CautionAlert( 500, 0L );
- }
-